<?php

    /*
     * Calculatte local tax, insurance and eco tax for nr_persons and nr nights
     */

    class tax_calculation {

        private $tax_row = array();
        private $nr_nights = 0;
        private $nr_adults = 0;
        private $nr_childrens = 0;
        private $ages = array();
        private $my_row = array();

        public function set_tax_row($tax_row) {
            $this->tax_row = $tax_row;
        }

        public function set_nr_nights($nr_nights) {
            $this->nr_nights = $nr_nights;
        }

        public function set_nr_adults($nr_adults) {
            $this->nr_adults = $nr_adults;
        }

        public function set_nr_childrens($nr_childrens) {
            $this->nr_childrens = $nr_childrens;
        }

        public function set_ages($ages) {
            $this->ages = $ages;
        }

        private function _check() {

            if (empty($this->tax_row))
                return array('success' => FALSE, 'msg' => 'Please give tax_row for tax_calculation class!');

            if ($this->nr_adults == 0)
                return array('success' => FALSE, 'msg' => 'Please give nr_adults for tax_calculation class!');

            if ($this->nr_nights == 0)
                return array('success' => FALSE, 'msg' => 'Please give nr_nights for tax_calculation class!');

            if (($this->nr_childrens != 0 && empty($this->ages)) || !is_array($this->ages))
                return array('success' => FALSE, 'msg' => 'Please give chidren ages as array for tax_calculation class!');

            return array('success' => TRUE, 'msg' => '');
        }

        public function calculate() {

            $chk = $this->_check();

            if (!$chk['success'])
                return $chk['msg'];


            $this->insurance();
            $this->tax();

            return $this->my_row;
        }

        private function insurance() {

            $this->my_row['insurance'] = $this->tax_row['insurance'] * $this->nr_nights * $this->nr_adults;

            if ($this->nr_childrens != 0)
                $this->my_row['insurance'] += $this->tax_row['insurance'] * $this->nr_nights * $this->nr_childrens;
        }

        /*
         * 0-12 - free
         * 12-18 - 50%
         * 18+ - 100%
         */

        private function tax() {

            $this->my_row['city'] = $this->tax_row['city'] * $this->nr_nights * $this->nr_adults;
            $this->my_row['eco'] = $this->tax_row['eco'] * $this->nr_nights * $this->nr_adults;


            foreach ($this->ages as $age):

                $age = (int) $age == 0 ? 1 : (int) $age;

                switch ($age) {
                    case $age >= 0 && $age < 12:


                        break;

                    case $age >= 12 && $age < 18:

                        $this->my_row['city'] += $this->tax_row['city'] * $this->nr_nights / 2;
                        $this->my_row['eco'] += $this->tax_row['eco'] * $this->nr_nights / 2;

                        break;

                    case $age >= 18:

                        $this->my_row['city'] += $this->tax_row['city'] * $this->nr_nights;
                        $this->my_row['eco'] += $this->tax_row['eco'] * $this->nr_nights;

                        break;
                }

            endforeach;
        }

    }
    